# coding:utf-8

from kgcar.model.neo4j_models import Neo4jConfig
import thulac
from kgcar.nlp.create_entity_dict import domain_entity_dict

# 预加载NLP处理包
thuFactory = thulac.thulac()

#预加载Neo4j图数据库
conn = Neo4jConfig()


def extract_entity(text):
	tag_list = thuFactory.cut(text, text=False) # [['分词', '词性'], ...]
	tag_list.append(['===', None])
	print(tag_list)
	# 命名实体词典
	label = domain_entity_dict
	entity_list = []
	i = 0
	n = len(tag_list)
	while i < n :
		phrase1 = tag_list[i][0]
		tag1 = tag_list[i][1]
		phrase2 = tag_list[i + 1][0]
		tag2 = tag_list[i + 1][1]
		phrase12 = phrase1 + phrase2
		# 基于组合识别实体对象：输出'分词一+分词二'、'分词二+分词三'、'分词三+分词四'...
		flag = conn.matchEntityItem(phrase12)
		if p12 in label and flag != None and preword(t1) and curword(t2):
			nerlist.append([p12,label[p12]])
			i += 2
			continue
		#单词对象识别：单词
		flag = conn.matchEntityItem(p1)
		if p1 in label and flag != None and curword(t1):	 # 词典内+数据库内+词性筛选（通过）
			nerlist.append([p1,label[p1]])
			i += 1
			continue
		#临时名词单词
		if tempword(t1):
			nerlist.append([p1,t1])
			i += 1
			continue
		nerlist.append([p1,0])
		i += 1
	return nerlist


text = "奥德赛是一辆好车。"
extract_entity(text)